﻿using System;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;

namespace SHA_SuRe
{
    /// <summary>
    /// Interaction logic for Editor.xaml
    /// </summary>
    public partial class Editor : UserControl
    {
        private MemoryStream stream;

        private bool saveFileInfo = false;
        public bool SaveFileInfo
        {
            get { return saveFileInfo; }
            set { saveFileInfo = value; }
        }


        public Editor()
        {
            InitializeComponent();
        }

        #region Set Text
        public void SetText(string text)
        {
            textBox.Text = text;
        }
        #endregion

        #region Get Text
        public string GetText()
        {
            return textBox.Text;
        }
        #endregion

        #region Event Click
        private void buttonNew_Click(object sender, RoutedEventArgs e)
        {
            clear();
        }

        private void buttonOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.RestoreDirectory = true;
            String fileName = null;

            // Show Open File Dialog
            if (ofd.ShowDialog() == true)
            {
                fileName = ofd.FileName;
                if (fileName != null)
                {
                    // read file
                    readFile(fileName);
                }
            }
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            String fileName = null;
            String safeFileName = null;

            sfd.RestoreDirectory = true;
            // Show save file dialog
            if (sfd.ShowDialog() == true)
            {
                fileName = sfd.FileName;
                safeFileName = sfd.SafeFileName;

                if (fileName != null)
                {
                    // Save file
                    saveFile(fileName, safeFileName);
                }
            }
        }
        #endregion

        #region File Management
        private void readFile(string fileName)
        {
            // Make File Stream
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            // Create temporary
            long length = fs.Length;
            byte[] temp = new byte[length];

            // Read File
            fs.Position = 0;
            fs.Read(temp, 0, (int)length);

            // Set stream
            stream = new MemoryStream(temp);

            // Set Text
            SetText(Helper.ArrayByteToString(stream.ToArray()));
        }

        private void clear()
        {
            textBox.Text = "";
        }

        private void saveFile(String fileName, String safeFileName)
        {
            if (fileName != null)
            {
                StringBuilder sb = new StringBuilder();

                if (saveFileInfo)
                {
                    // Set filename
                    sb.Append("File name : " + safeFileName + "\n");

                    // Set last modification
                    sb.Append("Last modification : " + DateTime.Now.ToString() + "\n");
                }

                // Set diggest
                sb.Append(GetText());

                // Convert to stream
                stream = new MemoryStream(Helper.StringToArrayByte(sb.ToString()));

                // Save
                SaveMemoryStream(stream, fileName);
            }
        }
        
        private void SaveMemoryStream(MemoryStream ms, String fileName)
        {
            FileStream outStream = File.OpenWrite(fileName);
            // Clear open file
            outStream.Flush();

            // Write
            ms.WriteTo(outStream);

            // Close
            outStream.Flush();
            outStream.Close();
        }
        #endregion
    }
}
